package PeterSheep.IndependentEvents;

import PeterSheep.AccountsManage.UBPlayer;
import PeterSheep.Kernel;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;

public class UAccountEvents implements Listener {
    @EventHandler
    private void playerAutoLogout(PlayerQuitEvent event) {
        event.setQuitMessage(null);
        Player logoutTarget = event.getPlayer();
        UBPlayer logoutAccount = Kernel.userAccountsList.searchPlayerFGameName(logoutTarget.getName());
        if(logoutAccount != null) {
            logoutAccount.getBindAccount().logOutAccount();
            Bukkit.broadcastMessage("§bPlayer §a>>> §7[§c-§7] §8" + logoutAccount.getBindPlayer().getName());
        }
    }

    @EventHandler
    private void playerLoginMessageFirst(PlayerJoinEvent event) {
        event.setJoinMessage(null);
        Player loginTarget = event.getPlayer();
        UBPlayer loginAccount = Kernel.userAccountsList.searchPlayerFGameName(loginTarget.getName());
        loginTarget.sendMessage("§6[UACT] §aTips: §eYou should login to play on this server!");
    }

    @EventHandler
    private void playerLoginMessageAction(PlayerInteractEvent event) {
        Player actionPlayer = event.getPlayer();
        UBPlayer actionPlayerAccount = Kernel.userAccountsList.searchPlayerFGameName(actionPlayer.getName());
        if(actionPlayerAccount == null) {
            event.setCancelled(true);
            actionPlayer.sendMessage("§6[UACT] §aTips: §eYou should login to break/place some block and use some block!");
        }
    }

    @EventHandler
    private void playerLoginMessageChat(AsyncPlayerChatEvent event) {
        Player actionPlayer = event.getPlayer();
        UBPlayer actionPlayerAccount = Kernel.userAccountsList.searchPlayerFGameName(actionPlayer.getName());
        if(actionPlayerAccount == null) {
            event.setCancelled(true);
            event.setMessage(null);
            actionPlayer.sendMessage("§6[UACT] §aTips: §eYou should login to use chat!");
        }
    }
}
